* This file prepares main tables for 'Benchmarking Intensity'

* Specifically, the output includes Tables 2, 3, 4, and 7
* Please refer to variable labels in the sample data for the input description

********************************************************************************
*** Load 
clear all
use sample_june_ret.dta, clear

gen lmv = log(total_mv)
gen band = 0
replace band = 1 if rank > c1 & rank < c2
gen band_d2 = 0
replace band_d2 = band*d02 

* choose band width
gen dist = 300

* set up the band width filter
gen filter = 0
replace filter = 1 if (abs(dist_up)<=dist | abs(dist_down)<=dist) 

* compute changes in BMI
gen d_bmi = bmi_june - bmi
gen d_bmi_act = bmi_act_june - bmi_act
gen d_bmi_pas = bmi_pas_june - bmi_pas

* construct intermediate BMI for Table 4
gen d_bmi_80 = 0.8*d_bmi_act + d_bmi_pas
gen d_bmi_60 = 0.6*d_bmi_act + d_bmi_pas
gen d_bmi_40 = 0.4*d_bmi_act + d_bmi_pas
gen d_bmi_20 = 0.2*d_bmi_act + d_bmi_pas

* choose controls
global full_controls " lmv ff_rus wcapm_beta ba_spread  band d02 band_d2 "

* set up consistent sample
qui reghdfe wjune_ret d_bmi $full_controls if filter == 1, absorb(PERMNO year) cl(PERMNO year)
gen byte full_panel = e(sample)

* demean within the sample
foreach lvar in $full_controls wjune_ret {
	bysort year: egen m_`lvar'_all = mean(`lvar') if filter == 1 & full_panel == 1
	gen dem_`lvar' = `lvar' - m_`lvar'_all
	replace dem_`lvar' = . if `lvar' == .
}

* construct BMI quartiles 
gen bmi_quartile = .
levelsof year, local(tempy)
foreach i in `tempy' {
	xtile ntile_temp = d_bmi if year == `i' & filter == 1 & full_panel == 1, nq(4)
	replace bmi_quartile = ntile_temp if missing(bmi_quartile)
	drop ntile_temp 
}

gen bmi1me = 0
gen bmi2me = 0
gen bmi3me = 0
gen bmi4me = 0

replace bmi1me = 1 if bmi_quartile == 1
replace bmi2me = 1 if bmi_quartile == 2
replace bmi3me = 1 if bmi_quartile == 3
replace bmi4me = 1 if bmi_quartile == 4


* percentage change in BMI
gen d_omega_me_p = d_bmi/bmi

label var d_bmi "Change in BMI"
label var bmi_quartile "Quartile of BMI change"
label var dem_wjune_ret "Return in June, demeaned"

label var bmi1me " 1($ \Delta BMI$ quartile 1) "
label var bmi2me " 1($ \Delta BMI$ quartile 2) "
label var bmi3me " 1($ \Delta BMI$ quartile 3) "
label var bmi4me " 1($ \Delta BMI$ quartile 4) "

global controls " lmv ff_rus band d02 band_d2 "
global controls_dem " dem_lmv dem_ff_rus dem_band dem_d02 dem_band_d2 "
global full_controls_dem " dem_lmv dem_ff_rus dem_wcapm_beta dem_ba_spread   dem_band dem_d02 dem_band_d2 "

xtset PERMNO year





********************************************************************************
*** Produce tables

**** Produce Table 2

	reghdfe wjune_ret d_bmi $controls if filter == 1 & full_panel == 1, absorb( year) cl(PERMNO year)

	reghdfe wjune_ret d_bmi $full_controls  if  filter == 1 & full_panel == 1, absorb( year) cl(PERMNO year)

	reghdfe wjune_ret d_bmi $full_controls  if filter == 1 & full_panel == 1, absorb(PERMNO year) cl(PERMNO year)

	ivreghdfe dem_wjune_ret bmi1me bmi2me bmi3me bmi4me $controls_dem  if filter == 1 & full_panel == 1,  nocons cl(PERMNO year)

	ivreghdfe dem_wjune_ret bmi1me bmi2me bmi3me bmi4me $full_controls_dem  if filter == 1 & full_panel == 1, nocons cl(PERMNO year)

	* Column (6) - percentage BMI change
	tab bmi_quartile if filter == 1 & full_panel == 1, summ(d_bmi)

	
	
	
**** Produce Table 3

	reghdfe wjune_ret deljm_IO $full_controls if filter == 1 & full_panel == 1 , absorb( year) cl(PERMNO year) 

	ivreghdfe wjune_ret (deljm_IO = d2) $full_controls if filter == 1 & full_panel == 1 , absorb( year) cl(PERMNO year) first 

	ivreghdfe wjune_ret (deljm_IO = d_bmi d2) $full_controls if filter == 1 & full_panel == 1 , absorb( year) cl(PERMNO year) first 
	* hansen j-test separately
	ivreghdfe wjune_ret (deljm_IO = d_bmi d2) $full_controls if filter == 1 & full_panel == 1 , absorb( year) robust 

	ivreghdfe wjune_ret (deljm_IO = d_bmi) $full_controls if filter == 1 & full_panel == 1 , absorb( year) cl(PERMNO year) first 

	ivreghdfe wapr_june_ret (deljm_IO = d_bmi) if filter == 1 & full_panel == 1 , absorb( year) cl(PERMNO year) first 




**** Produce Table 4

	gen bmi_type = d_bmi

	foreach x of varlist d_bmi d_bmi_80 d_bmi_60 d_bmi_40 d_bmi_20 d_bmi_pas {
		* column (1) estimates
		replace bmi_type = `x'
		reghdfe wjune_ret bmi_type $full_controls if filter == 1 & full_panel == 1, absorb( year) cl(PERMNO year)
		
		* column (2) estimates
		replace bmi_type = 0.5*`x'
		reghdfe wjune_ret bmi_type $full_controls if filter == 1 & full_panel == 1, absorb( year) cl(PERMNO year)
	}


	
	
**** Produce Table 7
gen after_2006 = 0
replace after_2006 = 1 if year > 2006
gen d_bmi_X_after_2006 = d_bmi*after_2006

foreach x of varlist wARet12 wARet24 wARet36 wARet48 wARet60 {
	* Panel A
	reghdfe `x' d_bmi $full_controls if filter == 1, absorb(PERMNO year) cl(PERMNO year)
	
	* Panel B
	reghdfe `x' d_bmi $full_controls if filter == 1, absorb(year) cl(PERMNO year)
	
	* Panel C
	reghdfe `x' d_bmi $controls if filter == 1, absorb(PERMNO year) cl(PERMNO year)
	
	* Panel D
	reghdfe `x' d_bmi $full_controls if (abs(dist_up)<=150 | abs(dist_down)<=150) , absorb(PERMNO year) cl(PERMNO year)
	
	* Panel E
	reghdfe `x' d_bmi d_bmi_X_after_2006 $full_controls if (abs(dist_up)<=150 | abs(dist_down)<=150) , absorb(PERMNO year) cl(PERMNO year)
	
	}


